/*
 * 太原科技大学。
 * Copyright 2021, YCHS Information&Science Technology Co.,Ltd,
 * All rights reserved.
 */
package com.example.demo.service.impl;


import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.example.demo.Utils.AliyunSmsSender;
import com.example.demo.Utils.HandleUtil;
import com.example.demo.Utils.MD5Util;
import com.example.demo.service.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author liuchao
 * @version 1.0
 * @date 2024/9/13
 */

@Service
public class CodeImpl implements Code {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    HandleUtil handleUtil;
    @Autowired
    AliyunSmsSender aliyunSmsSender;
    //图片验证码发送
    @Override
    public void setPicture(String[] message,Object Handle) {
        //将传来的信息数组遍历并通过字符串构建器进行重构
        StringBuilder mergedString = new StringBuilder();
        for (String str : message) {
            mergedString.append(str).append("-");
        }
        //加密用户签名作为redis的key
        String MD5key= MD5Util.encrypt(mergedString.toString());
        System.out.println(MD5key);
        System.out.println(mergedString.toString());
        //验证码缓存
        handleUtil.redisSet(MD5key,Handle,redisTemplate);

    }

    //短信验证码发送
    @Override
    public boolean setPhone(String[] message) {
        //验证码生成,使用getCode方法，参数为验证码位数
        String phoneCode=handleUtil.getCode(6);
        //将传来的信息数组遍历并通过字符串构建器进行重构
        StringBuilder mergedString = new StringBuilder();
        for (String str : message) {
            mergedString.append(str).append("-");
        }
        //加密用户签名作为redis的key
        String MD5key= MD5Util.encrypt(mergedString.toString());
        System.out.println(mergedString.toString());
        //验证码发送，先发送，后缓存，保证不会缓存到发送失败的废验证码
        try {
            SendSmsResponse response =aliyunSmsSender.smsSend(message[2],phoneCode);

            if (response.getBody()!= null) {
                System.out.println("请求 ID：" + response.getBody().getRequestId());
                System.out.println("状态码：" + response.getBody().getCode());
                System.out.println("消息：" + response.getBody().getMessage());
                System.out.println("业务 ID：" + response.getBody().getBizId());
            }
            //验证码缓存
            handleUtil.redisSet(MD5key,phoneCode,redisTemplate);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    //验证码验证
    @Override
    public boolean put(String[] message, String code) {
        //将传来的信息数组遍历并通过字符串构建器进行重构
        StringBuilder mergedString = new StringBuilder();
        for (String str : message) {
            mergedString.append(str).append("-");
        }
        //加密用户签名作为redis的key进行查询
        String MD5key= MD5Util.encrypt(mergedString.toString());
        System.out.println(MD5key);
        System.out.println(mergedString.toString());
        //验证码提取并比对
        String tmpCode=(String) redisTemplate.opsForValue().get(MD5key);
        if(code.equals(tmpCode)){
            return true;
        }else {
            return false;
        }

    }

}
